// pages/volunteer/order-detail.js
Page({
  data: {
    order: null,
    orderId: null
  },

  onLoad(options) {
    const orderId = parseInt(options.id);
    this.setData({ orderId });
    this.loadOrderDetail(orderId);
  },

  // 加载订单详情
  loadOrderDetail(orderId) {
    const orders = wx.getStorageSync('volunteerOrders') || [];
    const order = orders.find(o => o.id === orderId);
    
    if (order) {
      this.setData({ order });
    } else {
      wx.showToast({
        title: '订单不存在',
        icon: 'none'
      });
      setTimeout(() => {
        wx.navigateBack();
      }, 1500);
    }
  },

  // 获取状态图标
  getStatusIcon(status) {
    const iconMap = {
      'pending': '⏳',
      'shipped': '🚚',
      'completed': '✓'
    };
    return iconMap[status] || '❓';
  },

  // 获取状态描述
  getStatusDesc(status) {
    const descMap = {
      'pending': '订单已提交，正在处理中',
      'shipped': '商品已发货，正在配送中',
      'completed': '订单已完成，感谢您的支持'
    };
    return descMap[status] || '未知状态';
  },

  // 复制订单号
  copyOrderNo() {
    wx.setClipboardData({
      data: this.data.order.orderNo,
      success: () => {
        wx.showToast({
          title: '订单号已复制',
          icon: 'success'
        });
      }
    });
  },

  // 查看物流
  viewTracking() {
    if (this.data.order.trackingNumber) {
      wx.navigateTo({
        url: `/pages/volunteer/tracking?orderId=${this.data.orderId}&trackingNumber=${this.data.order.trackingNumber}`
      });
    } else {
      wx.showToast({
        title: '暂无物流信息',
        icon: 'none'
      });
    }
  },

  // 确认收货
  confirmReceived() {
    wx.showModal({
      title: '确认收货',
      content: '确认已收到商品吗？',
      confirmText: '确认',
      cancelText: '取消',
      success: (res) => {
        if (res.confirm) {
          this.updateOrderStatus('completed');
        }
      }
    });
  },

  // 取消订单
  cancelOrder() {
    wx.showModal({
      title: '取消订单',
      content: '确定要取消这个订单吗？取消后积分将退还到您的账户。',
      confirmText: '确定取消',
      cancelText: '再想想',
      success: (res) => {
        if (res.confirm) {
          this.cancelOrderConfirm();
        }
      }
    });
  },

  // 确认取消订单
  cancelOrderConfirm() {
    wx.showLoading({
      title: '处理中...',
      mask: true
    });

    setTimeout(() => {
      // 更新订单状态
      this.updateOrderStatus('cancelled');
      
      // 退还积分
      this.refundPoints();
      
      wx.hideLoading();
      wx.showToast({
        title: '订单已取消',
        icon: 'success'
      });
    }, 1000);
  },

  // 更新订单状态
  updateOrderStatus(status) {
    const orders = wx.getStorageSync('volunteerOrders') || [];
    const updatedOrders = orders.map(order => {
      if (order.id === this.data.orderId) {
        return {
          ...order,
          status: status,
          statusText: this.getStatusText(status)
        };
      }
      return order;
    });
    
    wx.setStorageSync('volunteerOrders', updatedOrders);
    
    // 更新页面数据
    this.setData({
      order: {
        ...this.data.order,
        status: status,
        statusText: this.getStatusText(status)
      }
    });
  },

  // 退还积分
  refundPoints() {
    const userData = wx.getStorageSync('volunteerUserData') || {};
    const newPoints = userData.currentPoints + this.data.order.points;
    
    const updatedUserData = {
      ...userData,
      currentPoints: newPoints
    };
    
    wx.setStorageSync('volunteerUserData', updatedUserData);
  },

  // 获取状态文本
  getStatusText(status) {
    const statusMap = {
      'pending': '待发货',
      'shipped': '已发货',
      'completed': '已完成',
      'cancelled': '已取消'
    };
    return statusMap[status] || '未知状态';
  },

  // 分享订单
  onShareAppMessage() {
    return {
      title: `我在爱道兑换了${this.data.order.productName}`,
      path: `/pages/volunteer/order-detail?id=${this.data.orderId}`,
      imageUrl: this.data.order.productImage
    };
  }
});
